﻿using ProtoBuf.Meta;
using SyncCore.Sync;
using SyncCore.Sync.Helpers;
using SyncCore.Sync.Models;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Text;

namespace SyncDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var host = new Peer();
            var player2 = new Peer();
            var player3 = new Peer();

            var players = new Peer[] { host, player2, player3 };

            host.CreateRoom();
            host.JoinRoom(host.HostServerAddressPortInfo);
            player2.JoinRoom(host.HostServerAddressPortInfo);
            player3.JoinRoom(host.HostServerAddressPortInfo);

            foreach (var p in players)
            {
                Console.WriteLine(p.ClientAddressPortInfo);
            }

            Thread.Sleep(2000); 
            host.LockRoom();

            //Console.ReadKey();

            host.SetReadyAndStartFrameIndex0();
            //Thread.Sleep(1000);
            player2.SetReadyAndStartFrameIndex0();
            //Thread.Sleep(1000);
            player3.SetReadyAndStartFrameIndex0();
            //Thread.Sleep(1000);
            Console.ReadKey();

            host.Dispose();
            player2.Dispose();
            //player3.Dispose();
        }


        static void Main2(string[] args)
        {
            var buffer = new byte[1024];
            var ms = new MemoryStream(buffer);
            var tm = TypeModelHelper.CreateMessagePackTypeModel();

            var pack = new MessagePack()
            {
                Type = MessageType.RPC,
                FrameIndex = 0,
                Commands = new CommandMessagePack[]{
                new CommandMessagePack() { Sender = "hh1", Command = "111" }
            }
            };

            ms.Seek(0, SeekOrigin.Begin);
            tm.SerializeWithLengthPrefix(ms, pack, typeof(MessagePack), ProtoBuf.PrefixStyle.Base128, 1);

            ms.Seek(0, SeekOrigin.Begin);
            var pack2 = tm.DeserializeWithLengthPrefix(ms, null, typeof(MessagePack), ProtoBuf.PrefixStyle.Base128, 1) as MessagePack;
            Console.WriteLine(pack2.FrameIndex);
        }
    }
}
